package cn.youyeku.zsxh.Utils;


import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import cn.youyeku.zsxh.app;


/**
 * 应用程序配置类：用于保存用户相关信息及设置
 */
public class AppConfig {

    //系统配置
    public static boolean CONF_RECEIVE_MESSAGE=true;
    public static boolean CONF_ENABLE_SOUNDS=false;
    public static boolean CONF_CHECK_UPDATE_FIRST=true;
    public static Boolean CONF_IS_TEACHER; //教师值判断

    public static String CONF_USER_ID;
    public static String CONF_USER_NICK_NAME;
    public static String CONF_USER_SIGN="";
    public static String CONF_USER_BIRTHDAY="";
    public static String CONF_USER_SEX="";



    public final static String APP_CONFIG = app.getAppPath() + "/conf/";
    public final static String APP_CONFIG_NAME = "setting.conf";


    public AppConfig() {
        LogUtils.e("initdata");
    }

    /**
     * 获取Preference设置
     */
    private SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(app.getAppContext());
    }

    public static void writeFile() {


    }

    public static void LoginOut() {
    File file = new File(app.getCookiePath());
    File file2 = new File(APP_CONFIG+APP_CONFIG_NAME);
        if(file.exists()){
            file.delete();
        }

        if (file2.exists()){
            file2.delete();
        }


    }

    public String getValuesFromKey(String key) {
        return getSharedPreferences().getString(key, "");
    }


    //Map<String, String> map = new HashMap<>();
    public void writeValuesToXml(HashMap<String, String> map) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.clear();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            editor.putString(entry.getKey(), entry.getKey());
        }
        editor.apply();

    }


    public static Properties getProps() {
        FileInputStream fis = null;
        Properties props = new Properties();
        try {
            // 读取files目录下的APP_CONFIG
            fis = new FileInputStream(new File(APP_CONFIG + APP_CONFIG_NAME));
            props.load(fis);

        } catch (Exception e) {
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }


        return props;
    }

    private static void setProps(Properties p) {
        FileOutputStream fos = null;
        try {

            File conf = new File(APP_CONFIG + APP_CONFIG_NAME);
            fos = new FileOutputStream(conf);
            p.store(fos, null);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }


    public static String get(String key) {
        Properties props = getProps();
        return (props != null) ? props.getProperty(key) : null;
    }

    public static void set(Properties ps) {
        Properties props = getProps();
        props.putAll(ps);
        setProps(props);
    }

    public static void set(String key, String value) {
        Properties props = getProps();
        props.setProperty(key, value);
        setProps(props);
    }

    public static void remove(String... key) {
        Properties props = getProps();
        for (String k : key)
            props.remove(k);
        setProps(props);
    }

}
